package com.universalInterface.lims_interface.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.universalInterface.common.exception.BusinessException;
import com.universalInterface.lims_interface.entity.TestChannel;
import com.universalInterface.lims_interface.mapper.LabInfoMapper;
import com.universalInterface.lims_interface.mapper.TestChannelMapper;
import com.universalInterface.lims_interface.service.ITestChannelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class TestChannelServiceImpl implements ITestChannelService {

	private final TestChannelMapper testChannelMapper;
	private final LabInfoMapper labInfoMapper;

	@Override
	public Integer saveBatchByTable(String tableName, List<TestChannel> entityList) {
		// 查询实验室是否存在
		List<Long> c = entityList.stream().map(x -> x.getLabGuid()).distinct().collect(Collectors.toList());
		QueryWrapper q = new QueryWrapper();
		q.in("lab_guid", c);
		List list = labInfoMapper.selectList(q);
		if (c.size() != list.size()) {
			throw new BusinessException(500, "实验室labGuid存在错误");
		}
		entityList.stream().forEach(x -> {
			x.setTestChGuid(IdUtil.getSnowflakeNextId());
		});
		Integer i = testChannelMapper.saveBatchByTable(tableName, entityList);
		return i;
	}
}
